package cn.project.lemontree.controller;

import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.Teacher;
import cn.project.lemontree.bean.User;
import cn.project.lemontree.service.Notice_PerService;
import cn.project.lemontree.service.TeacherService;
import cn.project.lemontree.tools.CommonResult;
import cn.project.lemontree.utils.MyAuthenticationToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/teacher")
@Api(tags = "老师管理模块")
@CrossOrigin
public class TeacherController {
    /**
     * 服务对象
     */
    @Resource
    private TeacherService teacherService;
    @Resource
    private Notice_PerService noticePerService;

    @ApiOperation("分页查询老师")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="teacherName",value="老师姓名",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name="telephone",value="电话号码",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult listpage(int page, int size, String teacherName,String telephone){
        Map<String,Object> map = new HashMap<>();
        map.put("count",teacherService.count(teacherName,telephone));
        map.put("data",teacherService.listpage(page,size,teacherName,telephone));
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "删除老师")
    @ApiOperation("删除老师信息")
    @GetMapping("/remove")
    public CommonResult remove(@RequestParam("teacherId") int teacherId){
        return CommonResult.success(teacherService.removeById(teacherId));
    }

    @SystemControllerLog(description = "添加老师")
    @ApiOperation(value = "添加老师信息")
    @PostMapping("/save")
    public  CommonResult saveTeacher(@RequestBody Teacher teacher){
        return CommonResult.success(teacherService.saveTeacher(teacher));
    }

    @ApiOperation(value = "老师登录")
    @PostMapping("/teacher_login")
    public CommonResult login(@RequestBody Teacher teacher){

        char[] pass = teacher.getPassword().toCharArray();
        MyAuthenticationToken token = new MyAuthenticationToken(teacher.getTelephone(),pass,1);
        Subject subject = SecurityUtils.getSubject();
        try{
            if(!subject.isAuthenticated()){
                subject.login(token);
            }
        }catch (UnknownAccountException e){
            return CommonResult.failed("用户名不存在");
        }catch (IncorrectCredentialsException e){
            return CommonResult.failed("密码不正确");
        }catch (AuthenticationException e){
            return CommonResult.failed();
        }
        return CommonResult.success(true);
    }

    @SystemControllerLog(description = "修改老师")
    @ApiOperation("修改老师信息")
    @PostMapping("/update")
    public CommonResult update(@RequestBody Teacher teacher){
        return CommonResult.success(teacherService.update(teacher));
    }

    @GetMapping("/notice")
    public CommonResult<Map<String ,Object>> searchNotice(Integer teacherId){
//        User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
        Map<String ,Object> map = new HashMap<>();
        map.put("data",noticePerService.searchNotice(teacherId,1));
        map.put("count",noticePerService.count(teacherId,1));
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "查找老师")
    @ApiOperation("查找老师信息")
    @GetMapping("/find")
    public CommonResult find(){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",teacherService.find());
        map.put("count",teacherService.count(null,null));
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "查找空闲老师")
    @ApiOperation("查找空闲老师信息")
    @GetMapping("/findfreeteachers")
    public CommonResult findFreeTeachers(){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",teacherService.findFreeTeachers());
        return CommonResult.success(map);
    }

    @ApiOperation("分页查询老师（附加班级信息）")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="teacherName",value="老师姓名",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name="telephone",value="电话号码",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/findteachersandclass")
    public CommonResult findTeachersAndClass(int page, int size, String teacherName,String telephone){
        Map<String,Object> map = new HashMap<>();
        map.put("count",teacherService.count(teacherName,telephone));
        map.put("data",teacherService.findTeachersAndClass(page,size,teacherName,telephone));
        return CommonResult.success(map);
    }
}

